package com.example.demo.service.impl;

import com.example.demo.dao.CustomerRepository;
import com.example.demo.entity.Customer;
import com.example.demo.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static org.springframework.transaction.annotation.Propagation.*;


/**
 * @auther yaoyouwei
 * @create 2020-03-25 22:07
 */
@Service("customerServiceAJpaImpl")
public class CustomerServiceAJpaImpl implements CustomerService {
    @Autowired
    CustomerRepository customerRepository;
    @Autowired
    CustomerServiceBJpaImpl serviceB;


    @Transactional(propagation = REQUIRED,rollbackFor = Exception.class)
    public void add1(Customer customer){
        customerRepository.save(customer);

        try {
             this.addInner(Customer.builder().lastName("yao").firstName("inner").build());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Transactional(propagation = REQUIRED,rollbackFor = Exception.class)
    public void add2(Customer customer){
        customerRepository.save(customer);
        try {
            this.addInner(Customer.builder().lastName("yao").firstName("inner").build());
        } catch (Exception e) {
            e.printStackTrace();
        }
        throwExc();
    }



    public void addInner(Customer customer) {
        customerRepository.save(customer);
    }

    public void addInnerExc(Customer customer) {
        customerRepository.save(customer);
        throwExc();
    }

    @Transactional(propagation = REQUIRED,rollbackFor = Exception.class)
    public void add4(Customer customer) {
        // 测试非注解方法
        customerRepository.save(customer);
        try {
            //serviceB.addWithoutTransactionalExc(Customer.builder().lastName("add4b").firstName("add4b").build());
            serviceB.addRequiredExc(Customer.builder().lastName("add4b").firstName("add4b").build());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void addWithoutTransactional(Customer customer) {
        customerRepository.save(customer);
    }

    @Override
    public void addWithoutTransactionalExc(Customer customer) {
        customerRepository.save(customer);
        throwExc();
    }

    @Override
    @Transactional(propagation = REQUIRED,rollbackFor = Exception.class)
    public void addRequired(Customer customer) {
        customerRepository.save(customer);
    }

    @Override
    @Transactional(propagation = REQUIRES_NEW,rollbackFor = Exception.class)
    public void addRequiredNew(Customer customer) {
        customerRepository.save(customer);
    }

    @Override
    @Transactional(propagation = NESTED,rollbackFor = Exception.class)
    public void addNested(Customer customer) {
        customerRepository.save(customer);
    }

    @Override
    @Transactional(propagation = SUPPORTS,rollbackFor = Exception.class)
    public void addSupports(Customer customer) {
        customerRepository.save(customer);
    }

    @Override
    @Transactional(propagation = NOT_SUPPORTED,rollbackFor = Exception.class)
    public void addNotSupported(Customer customer) {
        customerRepository.save(customer);
    }

    @Override
    @Transactional(propagation = MANDATORY,rollbackFor = Exception.class)
    public void addMandatory(Customer customer) {
        customerRepository.save(customer);
    }

    @Override
    @Transactional(propagation = NEVER,rollbackFor = Exception.class)
    public void addNever(Customer customer) {
        customerRepository.save(customer);
    }

    @Override
    @Transactional(propagation = REQUIRED,rollbackFor = Exception.class)
    public void addRequiredExc(Customer customer) {
        customerRepository.save(customer);
        throwExc();
    }

    @Override
    @Transactional(propagation = REQUIRES_NEW,rollbackFor = Exception.class)
    public void addRequiredNewExc(Customer customer) {
        customerRepository.save(customer);
        throwExc();
    }

    @Override
    @Transactional(propagation = NESTED,rollbackFor = Exception.class)
    public void addNestedExc(Customer customer) {
        customerRepository.save(customer);
        throwExc();
    }

    @Override
    @Transactional(propagation = SUPPORTS,rollbackFor = Exception.class)
    public void addSupportsExc(Customer customer) {
        customerRepository.save(customer);
        throwExc();
    }

    @Override
    @Transactional(propagation = NOT_SUPPORTED,rollbackFor = Exception.class)
    public void addNotSupportedExc(Customer customer) {
        customerRepository.save(customer);
        throwExc();
    }

    @Override
    @Transactional(propagation = MANDATORY,rollbackFor = Exception.class)
    public void addMandatoryExc(Customer customer) {
        customerRepository.save(customer);
        throwExc();
    }

    @Override
    @Transactional(propagation = NEVER,rollbackFor = Exception.class)
    public void addNeverExc(Customer customer) {
        customerRepository.save(customer);
        throwExc();
    }

    @Override
    public void throwExc() {
        int i = 1/0;
    }
}
